package com.security.system.controller;

import cn.hutool.system.UserInfo;
import com.security.Utils.UserContextUtils;
import com.security.system.dao.SysMenuDao;
import com.security.system.entity.SysRole;
import com.security.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("index")
public class IndexController extends UserContextUtils {

    @Autowired
    SysMenuDao menudao;

    @GetMapping("")
    public String getLogin(Map<Object,Object> map){
        map.put("username",getUser().getUsername());
        return "index/index";
    }

    @GetMapping("main")
    public String getMain(){
        return "index/main2";
    }

    @GetMapping("getMenu")
    @ResponseBody
    public Map<String,Object> getMenu(){
        Map<String,Object> res = new HashMap<>();
        List<SysRole> roles = getUser().getRoles();
        res.put("contentManagement",menudao.getMenu(roles.get(0).getRid().toString()));
        return res;
    }


    /*
    @GetMapping("menu")
    @ResponseBody
    public Map<String,Object> getmenu(){
        Map<String,Object> res = new HashMap<>();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        String username = "";
        if(principal instanceof UserInfo){
            UserInfo userInfo = (UserInfo)principal;
            List<UserRole> roles = userInfo.getRoles();
            res.put("contentManagement",dao.getMenu(roles.get(0).getId()+""));
        }else {
            username = principal.toString();
        }
        return res;
    }*/



}
